package com.example.hospitalsystem.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


import java.util.Collections;

/**
 * 开启cors资源共享
 */
@Configuration
@Slf4j
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){

        CorsConfiguration corsConfiguration=new CorsConfiguration();
        //允许任何来源
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
        //运行任何请求头
        corsConfiguration.addAllowedHeader(corsConfiguration.ALL);
        //允许任何请求方法
        corsConfiguration.addAllowedMethod(corsConfiguration.ALL);
        //允许任何凭证
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
